package com.hn.shiroredis.component;

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 自定义重写登录认证放方法
 * @author dnkj011
 *
 */
public class MyFormAuthenticationFilter extends FormAuthenticationFilter {

    @Override
    public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
    	//为了过滤跨域，先发送的那个请求
    	if (((HttpServletRequest) request).getMethod().toUpperCase().equals("OPTIONS")) {
    		return true;
            }
    	return super.isAccessAllowed(request, response, mappedValue);
    }
    /**
     * 未登陆直接返回消息提示
     */
    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletResponse res = (HttpServletResponse)response;
        res.setHeader("Access-Control-Allow-Origin", "*");
        res.setStatus(HttpServletResponse.SC_OK);
        res.setCharacterEncoding("UTF-8");
        PrintWriter writer = res.getWriter();
        //返回未登陆
        writer.write("token过期或未登录");
        writer.close();
        return false;
    }
}
